package com.w.wx.service.impls;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.github.binarywang.java.emoji.EmojiConverter;
import com.w.wx.domain.*;
import com.w.wx.mapper.*;
import com.w.wx.service.ChatService;
import com.w.wx.utils.ALToHMUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.*;

@Service
@Slf4j
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ChatListMapper chatListMapper;
    @Autowired
    private ChatMessageMapper chatMessageMapper;
    @Autowired
    private UserErMapper userErMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public void addInfo(String message) {

        ChatMessage msg= JSON.parseObject(message, ChatMessage.class);

        if (msg.getToOpenid().equals("-1")){
            return;
        }
        ChatList chatList = new ChatList();
        ChatList chatList1 = chatListMapper.selectByTwoOpenid(msg.getFromOpenid(),msg.getToOpenid());
        ChatList chatList2 = chatListMapper.selectByTwoOpenid(msg.getFromOpenid(),msg.getToOpenid());
        //目的:展示私信列表时，能看到发送的最后一条消息和时间
        if(chatList1==null&&chatList2==null){
            //第一次新增,同时插入双方的列表中
            UserEr userEr = userErMapper.selectByOpenid(msg.getFromOpenid());
            chatList.setFromOpenid(userEr.getOpenid());
            chatList.setFromHeadimage(userEr.getHeadImg());
            chatList.setFromState(userEr.getState());
            chatList.setFromName(userEr.getWxname());
            //设置表情
            chatList.setLastContext(ALToHMUtil.toHtml(msg.getContent()));
            chatList.setToOpenid(msg.getToOpenid());
            chatList.setSendTime(new DateTime());
            chatListMapper.insert(chatList);

            UserEr userEr1 = userErMapper.selectByOpenid(msg.getToOpenid());
            chatList.setFromOpenid(userEr1.getOpenid());
            chatList.setFromHeadimage(userEr1.getHeadImg());
            chatList.setFromState(userEr1.getState());
            chatList.setFromName(userEr1.getWxname());
            chatList.setToOpenid(msg.getFromOpenid());

            chatListMapper.insert(chatList);
        }else{
            //后面更新
            chatList1.setSendTime(new DateTime());
            chatList1.setLastContext(ALToHMUtil.toHtml(msg.getContent()));
            chatListMapper.updateByPrimaryKey(chatList1);
            chatList2.setSendTime(new DateTime());
            chatList2.setLastContext(ALToHMUtil.toHtml(msg.getContent()));
            chatListMapper.updateByPrimaryKey(chatList2);
        }



    }

    @Override
    public void addDeInfo(ChatMessage chatMessage) {
        chatMessage.setSendTime(new DateTime());
        chatMessage.setContent(ALToHMUtil.toHtml(chatMessage.getContent()));
        chatMessageMapper.insert(chatMessage);
    }

    @Override
    public ArrayList<ChatMessage> getAllNotRead(String fromOpenid,String toOpenid) {

        return chatMessageMapper.selectByOpenid(fromOpenid,toOpenid);
    }

    @Override
    public Msg getUserInfoList(String fromOpenid) {
        //查询别人向我发起的私信列表(u1是我想删除即)和查询我向别人发起的私信列表
       ArrayList<ChatList> chatLists = chatListMapper.selectByOpenid(fromOpenid);
        if (chatLists == null) {
            return Msg.fail();
        }else {
            return Msg.success().add("chatLists",chatLists);
        }

    }

    @Override
    public Msg getNotice(String openid,Integer type) {
        ArrayList<Notice> list =null;
        if(type == 0){
            list = noticeMapper.selectByOpenid(openid);
        }else {
            list = noticeMapper.selectByOpenid1(openid,"liked");
        }
        ArrayList<Map<String,Object>> list1 = new ArrayList<>();
        if (!list.isEmpty()){
            Iterator<Notice> it = list.iterator();
            while (it.hasNext()){
                Map<String,Object> map = new HashMap<>();
                Notice notice = it.next();
                UserEr userEr = userErMapper.selectByOpenid(notice.getFromOpenid());
                Goods goods = goodsMapper.selectDetailByGid(notice.getGid());
                if (goods != null && userEr !=null){
                    map.put("openid",userEr.getOpenid());
                    map.put("wxname",userEr.getWxname());
                    map.put("headImg",userEr.getHeadImg());
                    map.put("state",userEr.getState());
                    map.put("sendTime",notice.getSendTime());
                    map.put("content",ALToHMUtil.toUnicode(notice.getNoticeInfo()));
                    map.put("gid",notice.getGid());
                    map.put("goodsName",goods.getGoodsName());
                    map.put("goodsTitle",goods.getGoodsTitle());
                    list1.add(map);
                }
            }
        }
        return Msg.success().add("info",list1);
    }

    @Override
    public Msg deleteCommentsNotice(String openid, Integer type) {

        if (type==0){
            return ALToHMUtil.retrunEasy(noticeMapper.deleteByOpenid(openid));
        }else {
            return ALToHMUtil.retrunEasy(noticeMapper.deleteByOpenid1(openid));
        }

    }

    @Override
    public Msg deleteList1(String myOpenid, String beDeleteOpenid) {
        //我向别人发私信 from_openid就是我,别人向我to_openid就是我
        //通过自己openid和他人openid确定一行
        System.out.println(myOpenid+"  "+beDeleteOpenid);
        ChatList chatList = chatListMapper.selectByTwoOpenid(beDeleteOpenid, myOpenid);
        System.out.println(chatList);
        return ALToHMUtil.retrunEasy(chatListMapper.deleteByPrimaryKey(chatList.getListId()));
    }
}
